# 完全重写video_processor.py文件以解决语法错误
import os

# 文件路径
original_file = r'd:\未知\yolo_apply\video_processing\video_processor.py'
backup_file = r'd:\未知\yolo_apply\video_processing\video_processor.py.backup'

# 读取完整的备份文件内容
with open(backup_file, 'r', encoding='utf-8') as f:
    lines = f.readlines()

# 创建一个新的行列表，跳过有问题的区域
new_lines = []
in_problematic_area = False
problematic_start = False

for i, line in enumerate(lines):
    # 检测问题区域的开始
    if '# 错误率过高时触发警告和恢复' in line:
        problematic_start = True
        new_lines.append(line)
        # 添加正确的代码行，确保没有语法错误
        new_lines.append('                current_time = time.time()\n')
        new_lines.append('                if error_count > 10 and (current_time - last_error_time > 30):\n')
        new_lines.append('                    logger.warning("处理错误率过高: " + str(self.camera_id) + ", 最近错误数: " + str(error_count))\n')
        new_lines.append('                    if hasattr(self, "_handle_error"):\n')
        new_lines.append('                        self._handle_error(Exception("30秒内出现" + str(error_count) + "个错误"), "high_error_rate")\n')
        new_lines.append('                    last_error_time = current_time\n')
        new_lines.append('                    error_count = 0\n')
        in_problematic_area = True
    # 检测问题区域的结束
    elif in_problematic_area and 'error_count = 0' in line:
        in_problematic_area = False
    # 添加正常的行
    elif not in_problematic_area:
        new_lines.append(line)

# 写入重写后的内容
with open(original_file, 'w', encoding='utf-8') as f:
    f.writelines(new_lines)

print(f"已成功完全重写 {original_file} 文件")
print(f"问题区域已修复，现在应该没有语法错误了")